#!/usr/bin/clisp

(when (< (length *args*) 2)
    (error "Wrong usage, my-cp src dest"))

(let ( (src (elt *args* 0))
       (dest (elt *args* 1)) )

    (unless (probe-file src)
        (error "Source file ~a not exist" src))

    (when (probe-file dest)
        (error "Destination file ~a already exist" dest))

    (format t "Copying ~a to ~a~%" src dest)
    (with-open-file (in src :element-type '(unsigned-byte 8))
        (with-open-file (out dest 
                         :direction :output 
                         :element-type '(unsigned-byte 8))
            (let ( (buf (make-array 4096
                        :element-type (stream-element-type in))) )
                (loop for pos = (read-sequence buf in)
                    while (plusp pos)
                    do (write-sequence buf out :end pos))))))
